package com.tm.article.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tm.article.service.ApCommentService;
import com.tm.media.model.dtos.ApCommentDto;
import com.tm.media.model.entity.ApCommentEntity;
import com.tm.media.model.entity.ApCommentRepayEntity;
import com.tm.media.model.entity.ApUserEntity;
import com.tm.media.model.entity.ArticleEntity;
import com.tm.media.model.mapper.ApCommentMapper;
import com.tm.media.model.mapper.ApCommentRepayMapper;
import com.tm.media.model.mapper.ApUserMapper;
import com.tm.media.model.mapper.ArticleMapper;
import com.tm.media.utils.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Author Ly
 * @Data 2023/3/7 19:02
 * @ClassName ApCommentServiceImpl
 */
@Service
public class ApCommentServiceImpl extends ServiceImpl<ApCommentMapper, ApCommentEntity> implements ApCommentService {
    @Autowired
    private ApCommentMapper apCommentMapper;
    @Autowired
    private ApUserMapper apUserMapper;
    @Autowired
    private ArticleMapper articleMappera;
    @Autowired
    private RedisTemplate redisTemplate;
    @Autowired
    private ApCommentRepayMapper apCommentRepayMapper;
    @Override
    public Result pinglun(ApCommentDto apCommentDto) {
        ApCommentEntity apCommentEntity = new ApCommentEntity();
        //获取到当前用户的名称
        QueryWrapper<ApUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",apCommentDto.getComment_id());
        ApUserEntity apUserEntity = apUserMapper.selectOne(queryWrapper);
        //查一下频道ID
        QueryWrapper<ArticleEntity> channelQueryWrapper = new QueryWrapper<>();
        channelQueryWrapper.eq("id",apCommentDto.getArticle_id());
        ArticleEntity articleEntity = articleMappera.selectOne(channelQueryWrapper);
        if (apCommentDto.getBei_comment_id()==null){
            apCommentEntity.setContent(apCommentDto.getContent());
            apCommentEntity.setAuthorId(apCommentDto.getComment_id());
            apCommentEntity.setAuthorName(apUserEntity.getName());
            apCommentEntity.setEntryId(apCommentDto.getArticle_id());
            apCommentEntity.setChannelId(articleEntity.getChannelId());
            apCommentEntity.setType(0);
            apCommentEntity.setLikes(0);
            apCommentEntity.setFlag(0);
            apCommentEntity.setCreatedTime(LocalDateTime.now());
            apCommentMapper.insert(apCommentEntity);
        }else {
            ApCommentRepayEntity apCommentRepayEntity = new ApCommentRepayEntity();
            apCommentRepayEntity.setLikes(0);
            apCommentRepayEntity.setContent(apCommentDto.getContent());
            apCommentRepayEntity.setCommentId(apCommentDto.getComment_id());
            apCommentRepayEntity.setAuthorId(apCommentDto.getComment_id());
            apCommentRepayEntity.setAuthorName(apUserEntity.getName());
            apCommentRepayEntity.setCreatedTime(LocalDateTime.now());
            apCommentRepayMapper.insert(apCommentRepayEntity);
        }
        return null;
    }

    @Override
    public Result selectApComment() {
        List<ApCommentEntity> apCommentEntities = apCommentMapper.selectApComment();
        JSONArray jsonArray = JSONArray.parseArray(JSON.toJSONString(apCommentEntities));
        redisTemplate.opsForList().leftPush("评论2",jsonArray);
        List list = redisTemplate.opsForList().range("评论2", 0, -1);
        return Result.SUCCESS(list);
    }
}
